﻿using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class BossController : MonoBehaviour
{
    AudioSource audioSource;
    public Image image;
    RectTransform rect;
    public AudioClip[] audioClip= { };

    float speed = 0.5f;
    float speedFactor = 1.0f;
    private AIPath aiPath;
    public Animator animator;
    public MonsterAttackSensor attackSensor;

    float burnGap = 1f;
    float attackGap = 0;
    float birthGap = 9f;
    bool canmakesound = true;
    float makesoundGap = 5f;

    int maxhp = 3000;
    int hp = 3000;
    float orilenth;
    bool burn = false;    
    public MonsterManager monsterManager;

    public bool canAttack = false;
    LoadSceneParameters parameter;
    private void Stopplaying()
    {
        SceneManager.LoadScene("end");
    }
    public void GetHurt(int hurt)
    {
        Debug.Log("Get Hurt");
        if (canmakesound)
        {
            audioSource.PlayOneShot(audioClip[0], 0.2f);
            makesoundGap = 5f;
            canmakesound = false;
        }
        hp -= hurt;
        Debug.Log(hp);
        if (hp <= 0)
        {
            animator.SetTrigger("Die");
            Invoke("Stopplaying", 2.0f);
            //transform.localPosition = new Vector3(1000, 1000, 0);
            //Destroy(gameObject);
        }
    }

    private void updateGap()
    {

    }

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        aiPath = gameObject.GetComponent<AIPath>();
        animator = gameObject.GetComponent<Animator>();
        attackSensor = gameObject.GetComponentInChildren<MonsterAttackSensor>();
    }

    // Start is called before the first frame update
    void Start()
    {
        rect = image.GetComponent<RectTransform>();
        orilenth = rect.rect.width;
        aiPath.endReachedDistance = 0.5f + Random.value;
        aiPath.maxSpeed = speed * speedFactor;
    }

    // Update is called once per frame
    void Update()
    {
        image.fillAmount = 1.0f * hp / maxhp;
        if (burnGap > 0)
        {
            burnGap -= Time.deltaTime;
        }
        if (attackGap > 0)
        {
            attackGap -= Time.deltaTime;
        }
        if (birthGap > 0) {
            birthGap -= Time.deltaTime;
        }
        if (makesoundGap > 0 && !canmakesound){
            makesoundGap -= Time.deltaTime;
        }
        Vector3 vec = aiPath.desiredVelocity;
        animator.SetFloat("Mag", vec.x * vec.x + vec.y * vec.y);
        if (vec.x >= 0)
        {
            animator.SetFloat("X", 1f);
        }
        else {
            animator.SetFloat("X", 0f);
        }
        if (makesoundGap < 0)
        {
            makesoundGap = 5f;
            canmakesound = true;
        }
        if (attackSensor.targetNum > 0 && attackGap <= 0)
        {
            audioSource.PlayOneShot(audioClip[1]);
            animator.SetTrigger("Attack");
            attackGap = 3f;
        }
        if (birthGap < 0) {
            animator.SetTrigger("Birth");
            birthGap = 9f;
        }
        if (burn && burnGap <= 0)
        {
            GetHurt(10);
            burnGap = 1f;
        }
    }

    private void OnAttackEnter()
    {
        canAttack = false;
        aiPath.canMove = false;
    }

    private void OnAttackExit()
    {
        canAttack = true;
        aiPath.canMove = true;
        attackSensor.hurtTargetTable(40);        
    }

    private void OnDieEnter()
    {
        canAttack = false;
        aiPath.canMove = false;
    }

    private void OnDieExit()
    {        
    }

    private void OnBirthEnter() {
        aiPath.canMove = false;
    }

    private void OnBirthExit() {
        aiPath.canMove = true;
        attackSensor.hurtTargetTable(40);
        for (int i = 0; i < 2; ++i) {
            monsterManager.addMonster(transform.localPosition);
        }
    }

    private void EnterTree(bool burn)
    {
        this.burn = burn;
        //aiPath.maxSpeed = 0.4f * speed;
    }

    private void ExitTree()
    {
        burn = false;
        //aiPath.maxSpeed = speed;
    }

    private void EnterWater()
    {
        //aiPath.maxSpeed = 0.4f * speed;
    }

    private void ExitWater()
    {
        //aiPath.maxSpeed = speed;
    }
}
